﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using XWord.Common;
using Microsoft.Practices.ServiceLocation;
using Caliburn.PresentationFramework.ApplicationModel;
using XWord.Fundamental.Domain.Model;
using System.Threading;
using XWord.Fundamental.Domain;
using Caliburn.Core.Metadata;
using System.Collections.ObjectModel;
using Caliburn.PresentationFramework;
using XWord.WPF.ApplicationFramework;
namespace XWord.GUI.Presenters
{
    [Singleton(typeof(CategoryMasterPresenter))]
    public class CategoryMasterPresenter : Presenter
    {
        private readonly IWindowManager _windowManager;
        private readonly IServiceLocator _serviceLocator;
        public CategoryMasterPresenter(IServiceLocator entityFactory)    
        {
            _serviceLocator = entityFactory;
            _windowManager = entityFactory.GetInstance<IWindowManager>();
            Model = _serviceLocator.GetInstance<ICategoryModel>();  
        }

        public ICategoryModel Model { get; set; }

        protected override void OnInitialize()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            {
                CategoryList = Model.GetCategoryList().ToObservableCollection();
            }));
            
            base.OnInitialize();
        }

        private IObservableCollection<Category> _categoryList;
        public IObservableCollection<Category> CategoryList
        {
            get { return _categoryList ?? (_categoryList = new BindableCollection<Category>()); }
            set
            {
                _categoryList = value;
                NotifyOfPropertyChange("CategoryList");
            }
        }

        private Category _selectedItem;
        public Category SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                NotifyOfPropertyChange("SelectedItem");
            }
        }

        public void OpenNewDialog()
        {
            OpenDialog(false);
        }

        public void OpenEditDialog()
        {
            OpenDialog(true);
        }

        private void OpenDialog(bool IsNew)
        {
            //var presenter = _serviceLocator.GetInstance<CategoryEditPresenter>();
            //this.ActiveScreen = presenter;
            //if (IsNew)
            //{
            //    presenter.Entity = SelectedItem;
            //    presenter.InitializeEditEntity();
            //}
            //else
            //{
            //    presenter.InitializeNewEntity();
            //}
            //_windowManager
            //    .ShowDialog(presenter, null, ExecuteShutdownModel);
        }

        public void Delete()
        {
            Model.DeleteCategory(SelectedItem);
            CategoryList.Remove(SelectedItem);
            
        }
    }
}
